<?
  class CFD_Controller_Action extends Zend_Controller_Action
  {
    public function init()
	{
      $userSession = new Zend_Session_Namespace('user');
      $this->view->user = $this->user = $userSession->user;

	  $this->view->cart = $this->cart = CFD_Cart::getCartObj();

	  if ($this->user->type != "admin")
	    if (isset($this->user->user_enabled) && !$this->user->user_enabled)
	    {
	      if ($_SERVER['REQUEST_URI'] != '/account/activate' && $_SERVER['REQUEST_URI'] != '/account/logout')
	        $this->_redirect('/account/activate');
	    }
	}

	public function preDispatch()
	{
	  if ($this->view->user->id)
	  {
	    $messagesTable = new Application_Model_Messages();
		$this->view->unreadMsgCount = $messagesTable->getUnreadCount($this->user->id);
	  }
	  $this->view->unreadMsgCount = (int)$this->view->unreadMsgCount;
	}

	/**
	 * isLogged - check if user is logged in
	 * 
	 * @param bool $redirect - redirect to login screen if not
	 * @access public
	 * @return bool
	**/
	public function isLogged($redirect = false)
	{
   	   if (!$this->view->user->type)
	   {
   	     if ($redirect)
  		   $this->_redirect('/account/login/');
		 else
	       return false;
	   }

	   return true;
	}

	/**
	 * isAdmin - check if user is administrator
	 * 
	 * @param bool $redirect - redirect to login screen if not
	 * @access public
	 * @return bool
	**/
	public function isAdmin($redirect = false)
	{
   	   if ($this->view->user->type != "admin" && $redirect)
  		 $this->_redirect('/account/login/');

	   return $this->view->user->type == "admin";
    }
  }
